package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.hfinterface.BtnClickCallBack;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerHouseRentingShowDuanZuListComponent;
import com.kmproject.haifu.mvp.contract.HouseRentingShowDuanZuListContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HFLocationBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseRentListParmarBean;
import com.kmproject.haifu.mvp.presenter.HouseRentingShowDuanZuListPresenter;
import com.kmproject.haifu.mvp.ui.adapter.HouseRentingHeZuShowListAdapter;
import com.kmproject.haifu.mvp.ui.adapter.HouseRentingShowListAdapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectAddress1Adapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectPxAdapter;
import com.kmproject.haifu.mvp.ui.adapter.PopuWindowSelectSxAdapter;
import com.kmproject.haifu.mvp.ui.holder.AddressSelectHolder;
import com.kmproject.haifu.mvp.ui.holder.HxSelectHolder;
import com.kmproject.haifu.mvp.ui.holder.PxSelectHolder;

import com.kmproject.haifu.mvp.ui.holder.SxSelectHolderNew;
import com.kmproject.haifu.mvp.ui.holder.ZjSelectHolder;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.xw.repo.XEditText;
import com.yyydjk.library.DropDownMenu;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/04/2019 09:33
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HouseRentingShowDuanZuListActivity extends BaseActivity<HouseRentingShowDuanZuListPresenter> implements HouseRentingShowDuanZuListContract.View {

    @BindView(R.id.include_title_search_back)
    ImageView includeTitleSearchBack;
    @BindView(R.id.include_title_search_xedit)
    XEditText includeTitleSearchXedit;
    @BindView(R.id.include_title_search_msg)
    ImageView includeTitleSearchMsg;
    @BindView(R.id.include_title_search_lay)
    LinearLayout includeTitleSearchLay;
    RecyclerView HouseRentingShowDuanZuListRecyclerView;
    SmartRefreshLayout HouseRentingShowMainRefreshLayout;
    DropDownMenu dropDownMenu;
    private String headers[] = {"位置", "租金", "筛选", ""};
    private List<View> popupViews = new ArrayList<>();
    private View addressView, zuJinView, shaiXuanViewNew, paiXuView, contentView;
    private List<HousePayWayBean> addressSelectData, czyqAndfytsSelectData, zjqjHousePayWayBeans,sxHousePayWayBeans_new,addRessHousePayWayBeansDis;
    private AddressSelectHolder addressSelectHolder;
    private ZjSelectHolder zjSelectHolder;
    private PxSelectHolder pxSelectHolder;
    private SxSelectHolderNew sxSelectHolderNew;
    private HousePayWayBean addressSelectOne;//选择地址，被选中的那一个
    private HousePayWayBean addressSelectFj ;//选择附近 和选择地址互斥，
    private String msamllPrice = "", mbigPrice = "";//选择的最小最大价格
    private HousePayWayBean huXingSelectOne;//当前选中哪一个户型；如果是空的就是选中默认
    private String pxSelect = "";//排序选中的条件，默认的是空的
    private List<String> rentDetailList = new ArrayList<>();//出租要求，房源特色 等筛选条件取数据字典id
    public static boolean isOpen = true;//控制筛选时候的开关
    private HouseRentListParmarBean houseRentListParmarBean = new HouseRentListParmarBean();//查询条件对象
    private HouseRentingShowListAdapter houseRentingShowListAdapter;
//    private HouseRentingHeZuShowListAdapter houseRentingHeZuShowListAdapter;
    private HFLocationBean mbdLocation;
  private HousePayWayBean housePayWayBeanType;//类型，上一个传过来的
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHouseRentingShowDuanZuListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_house_renting_show_duan_zu_list; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter.getHousePayWayBean( Api.DISTANCE,"",addRessHousePayWayBeansDis);
        if (getIntent().hasExtra("HousePayWayBean"))
        {
            housePayWayBeanType=(HousePayWayBean)getIntent().getSerializableExtra("HousePayWayBean");
            houseRentListParmarBean.setHouseCategory(housePayWayBeanType.getParentCategory());
            headers[2]=housePayWayBeanType.getName();
        }

        isOpen = true;
        initDropDownMenu();
        mPresenter.getHousePayWayBean(Api.HOUSE_AREA, "", addressSelectData);//获取区域
        houseRentListParmarBean.setRentType(getIntent().getStringExtra("type"));
        mPresenter.getHousePayWayBean(Api.HOUSE_RENT_DETAIL+getIntent().getStringExtra("type").toUpperCase(), "", czyqAndfytsSelectData);//出租要求和房源特色
        if (housePayWayBeanType!=null)
        {
            mPresenter.getQueryConditionTree(housePayWayBeanType.getParentCategory(),"");
        }

        initPopuwindows();

        includeTitleSearchXedit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    HouseRentListParmarBean temhouseRentListParmarBean = new HouseRentListParmarBean();
                    if (housePayWayBeanType!=null)
                    {
                        houseRentListParmarBean.setHouseCategory(housePayWayBeanType.getParentCategory());
                    }
                    if (houseRentingShowListAdapter!=null)
                    {
                        houseRentingShowListAdapter.setNewData(null);
                    }
                    temhouseRentListParmarBean.setRentType(getIntent().getStringExtra("type"));
                    temhouseRentListParmarBean.setKeywords(s.toString());
                    mPresenter.getHouseRentList(true, temhouseRentListParmarBean);
                }
            }
        });

        if (getIntent().hasExtra("searchContent")) {
            includeTitleSearchXedit.setText(getIntent().getStringExtra("searchContent"));
            //带了搜索条件过来，直接搜索
        } else {
            //没带搜索条件，只有索索类型和分页的参数，还有默认区域
            HousePayWayBean housePayWayBean=DataHelper.getDeviceData(this,AppConstant.user_default_select_area);
            if (housePayWayBean!=null)
            {
                dropDownMenu.setTabText(housePayWayBean.getName(),0);
                if (housePayWayBean.getDictId()!=null&&!TextUtils.isEmpty(housePayWayBean.getDictId()))
                {
                    houseRentListParmarBean.setHouseArea(housePayWayBean.getDictId());
                }
                addressSelectOne=housePayWayBean;//默认选中的地址
            }

            mPresenter.getHouseRentList(false, houseRentListParmarBean);
        }
        mPresenter.getHousePayWayBean(Api.MONTH_RENT,"",zjqjHousePayWayBeans);//租金区间
    }

    /**
     * 初始化弹出控件
     */
    public void initDropDownMenu() {
        dropDownMenu = (DropDownMenu) findViewById(R.id.dropDownMenu);
        addressView = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_adress, null);
        zuJinView = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_zj, null);
        shaiXuanViewNew = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_sx_new, null);
        paiXuView = LayoutInflater.from(this).inflate(R.layout.popuwindows_seelct_px, null);
        popupViews.add(addressView);
        popupViews.add(zuJinView);
        popupViews.add(shaiXuanViewNew);
        popupViews.add(paiXuView);
        contentView = LayoutInflater.from(this).inflate(R.layout.house_renting_show_duanzu_list_content, null);
        HouseRentingShowDuanZuListRecyclerView = contentView.findViewById(R.id.HouseRentingShowDuanZuList_RecyclerView);
        HouseRentingShowMainRefreshLayout = contentView.findViewById(R.id.HouseRentingShowMain_refreshLayout);
        HouseRentingShowDuanZuListRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            houseRentingShowListAdapter = new HouseRentingShowListAdapter( new ArrayList<>());
            HouseRentingShowDuanZuListRecyclerView.setAdapter(houseRentingShowListAdapter);
            houseRentingShowListAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        HouseRentingShowMainRefreshLayout.setEnableRefresh(false);//禁止下拉刷新
        dropDownMenu.setMenuSelectedImgIcon(R.drawable.houserentingshow_selection_select_tj);
        dropDownMenu.setMenuUnselectedImgIcon(R.drawable.houserentingshow_selection_noselect_tj);
        dropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
        dropDownMenu.setClickBtn(new DropDownMenu.ClickBtn() {
            @Override
            public void clikCallBack(View view) {
                if (addressSelectData==null)
                {
                    mPresenter.getHousePayWayBean(Api.HOUSE_AREA, "", addressSelectData);//获取区域
                }
                if (addRessHousePayWayBeansDis==null)
                {
                    mPresenter.getHousePayWayBean( Api.DISTANCE,"",addRessHousePayWayBeansDis);
                }
                if (housePayWayBeanType!=null&&sxHousePayWayBeans_new==null)
                {
                    mPresenter.getQueryConditionTree(housePayWayBeanType.getParentCategory(),"");
                }
            }
        });
    }

    /**
     * 初始化弹框
     */
    private void initPopuwindows() {


        if (pxSelectHolder == null) {
            pxSelectHolder = new PxSelectHolder(this, paiXuView, new PopuWindowSelectPxAdapter.BtncliickCallBack() {
                @Override
                public void callBack(int index, Object content) {
                    if (houseRentingShowListAdapter!=null)
                    {
                        houseRentingShowListAdapter.setNewData(null);
                    }
                    pxSelect = (String) content;
                    houseRentListParmarBean.setOrderByFiled(pxSelect);
                    mPresenter.getHouseRentList(true, houseRentListParmarBean);
                    dropDownMenu.closeMenu();
//                    popup.dismiss();
                }
            });
        }
        HouseRentingShowMainRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //上拉刷新数据
                houseRentListParmarBean.setLimit(String.valueOf(Integer.parseInt(houseRentListParmarBean.getLimit()) + 1));
                mPresenter.getHouseRentList(false, houseRentListParmarBean);
            }
        });

    }

    @Override
    public void onBackPressed() {
        //退出activity前关闭菜单
        if (dropDownMenu.isShowing()) {
            dropDownMenu.closeMenu();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.popuwindows_seelct_sx_clearTv, R.id.popuwindows_seelct_sx_sureTv, R.id.include_title_search_back, R.id.include_title_search_msg})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.include_title_search_back:
                killMyself();
                break;
            case R.id.include_title_search_msg:
               AppUtil.gotoMessage(this);
                break;

            case R.id.popuwindows_seelct_sx_clearTv:
//                landlordTypeList.clear();
//                rentDetailList.clear();
//
//                houseRentListParmarBean.setLandlordTypeList(landlordTypeList);
//                houseRentListParmarBean.setRentDetailList(rentDetailList);
//                mPresenter.getHouseRentList(true, houseRentListParmarBean);
//                drawer.closeDrawers();
//                //用户点了清楚的时候还原条件，点了确认暂时没有还原
//                mPresenter.reductionStutas(mhouseInfoTitleBeans);
                break;
            case R.id.popuwindows_seelct_sx_sureTv:
//                landlordTypeList.clear();
//                rentDetailList.clear();
//                List<String> fySelect = mPresenter.getparmar(Api.LANDLORD_TYPE, mhouseInfoTitleBeans);
//                if (!fySelect.isEmpty()) {
//                    landlordTypeList.addAll(fySelect);
//                }
//                List<String> pzAndldSelect = mPresenter.getparmar(Api.HOUSE_RENT_DETAIL_DZ, mhouseInfoTitleBeans);
//                if (!pzAndldSelect.isEmpty()) {
//                    rentDetailList.addAll(pzAndldSelect);
//                }
//                houseRentListParmarBean.setLandlordTypeList(landlordTypeList);
//                houseRentListParmarBean.setRentDetailList(rentDetailList);
//                mPresenter.getHouseRentList(true, houseRentListParmarBean);
//                drawer.closeDrawers();


                break;
        }
    }

    @Override
    public Context myContext() {
        return this;
    }


    @Override
    public void getHousePayWayBeanSuccess(String value, List<HousePayWayBean> housePayWayBeans) {

         if (value.equals(Api.HOUSE_RENT_DETAIL+getIntent().getStringExtra("type").toUpperCase()))
         {
             czyqAndfytsSelectData =housePayWayBeans;
         }
        switch (value) {

            case Api.DISTANCE:
                addRessHousePayWayBeansDis=housePayWayBeans;
                break;
            case Api.MONTH_RENT:
                zjqjHousePayWayBeans =housePayWayBeans;
                if (zjSelectHolder == null) {
                    zjSelectHolder = new ZjSelectHolder(this,housePayWayBeans,zuJinView, new ZjSelectHolder.PriceCallback() {
                        @Override
                        public void callBack(String samllPrice, String bigPrice) {
                            msamllPrice = samllPrice;
                            mbigPrice = bigPrice;
                            houseRentListParmarBean.setPage("1");
                            if (TextUtils.isEmpty(samllPrice))
                            {
                                houseRentListParmarBean.setMinMonthRent(null);
                            }else {
                                houseRentListParmarBean.setMinMonthRent(samllPrice);
                            }
                            if (TextUtils.isEmpty(bigPrice))
                            {
                                houseRentListParmarBean.setMaxMonthRent(null);
                            }else {
                                houseRentListParmarBean.setMaxMonthRent(mbigPrice);
                            }
                            if (houseRentingShowListAdapter!=null)
                            {
                                houseRentingShowListAdapter.setNewData(null);
                            }
                            mPresenter.getHouseRentList(true, houseRentListParmarBean);
                            dropDownMenu.closeMenu();
                        }
                    });
                }
                break;
            case Api.HOUSE_AREA:
                this.addressSelectData = housePayWayBeans;
                if (addressSelectHolder == null && housePayWayBeans != null && !housePayWayBeans.isEmpty()&&addRessHousePayWayBeansDis!=null) {
                    //请求到地址后默认加载数据
                    addressSelectHolder = new AddressSelectHolder(this, addressView, housePayWayBeans,addRessHousePayWayBeansDis,addressSelectOne);
                    addressSelectHolder.init(new AddressSelectHolder.SelectAddressCallBack() {
                        @Override
                        public void callBack(String type, int index, Object o) {
                            dropDownMenu.closeMenu();
                            if (houseRentingShowListAdapter!=null)
                            {
                                houseRentingShowListAdapter.setNewData(null);
                            }
                            if (index==0)
                            {
                                dropDownMenu.setTabText("不限",0);
                                houseRentListParmarBean.setDis(null);
                                houseRentListParmarBean.setHouseArea(null);
                                houseRentListParmarBean.setLgt(null);
                                houseRentListParmarBean.setLat(null);
                                houseRentListParmarBean.setPage("1");
                                mPresenter.getHouseRentList(true,houseRentListParmarBean);
                                return;
                            }
                            if (type.equals("区域")) {
                                addressSelectOne = (HousePayWayBean) o;
                                addressSelectFj = null;
                                dropDownMenu.setTabText((type.equals("区域") ? ((HousePayWayBean) o).getName() : type),0);
                                houseRentListParmarBean.setDis(null);
                                houseRentListParmarBean.setHouseArea(addressSelectOne.getDictId());
                                houseRentListParmarBean.setLgt(null);
                                houseRentListParmarBean.setLat(null);

                            } else {
                                dropDownMenu.setTabText("附近",0);
                                addressSelectOne = null;
                                addressSelectFj = (HousePayWayBean) o;
                                if (!addressSelectFj.getName().equals("不限"))
                                {
                                    houseRentListParmarBean.setDis(addressSelectFj.getCategory());
                                }else {
                                    houseRentListParmarBean.setDis(null);
                                }
                                houseRentListParmarBean.setHouseArea(null);
                                Location location=startLocation();
                                if (location!=null)
                                {
                                    houseRentListParmarBean.setLgt(String.valueOf(location.getLongitude()));
                                    houseRentListParmarBean.setLat(String.valueOf(location.getLatitude()));

                                }else {
                                    showMessage("未获取到经纬度信息");
                                }
                               //必须设置经纬度
                            }
                            mPresenter.getHouseRentList(true,houseRentListParmarBean);
                            dropDownMenu.closeMenu();
                        }
                    });
                }
                break;
        }
    }

    @Override
    public void getHouseRentListSuccess(ResultBean<List<HouseRentListShowBean>> houseRentListShowBeans) {
        HouseRentingShowMainRefreshLayout.finishLoadMore();
        if (houseRentListShowBeans.getData() == null || houseRentListShowBeans.getData().isEmpty()) {
            HouseRentingShowMainRefreshLayout.setEnableLoadMore(false);//不能刷新了
                if (houseRentingShowListAdapter.getItemCount() < 1) {
                    //没有数据提示
                    houseRentingShowListAdapter.setEmptyView(AppUtil.setEmptyView(this, null, null, new BtnClickCallBack() {
                        @Override
                        public void callback(View view) {
                            mPresenter.getHouseRentList(true, houseRentListParmarBean);
                        }
                    }, false));

                    return;
                }


            //当前请求没有数据
            return;
        }

        if (houseRentListShowBeans.getData().size() < AppConstant.pageSize) {
            //如果当前获取的数据的条数小于当前每页显示条数，不能刷新了
            HouseRentingShowMainRefreshLayout.setEnableLoadMore(false);//不能刷新了
        }
            houseRentingShowListAdapter.addData(houseRentListShowBeans.getData());


    }

    @Override
    public HouseRentingShowListAdapter getHouseRentingShowListAdapter() {
        return houseRentingShowListAdapter;
    }


    @Override
    public void getQueryConditionTreeSuccess(List<HousePayWayBean> housePayWayBeans) {
        //筛选条件会员
        sxHousePayWayBeans_new=housePayWayBeans;
        sxSelectHolderNew=new SxSelectHolderNew(this, shaiXuanViewNew, new SxSelectHolderNew.selectCallBack() {
            @Override
            public void callBack(HousePayWayBean housePayWayBean) {
                dropDownMenu.closeMenu();
                if (houseRentingShowListAdapter!=null)
                {
                    houseRentingShowListAdapter.setNewData(null);
                }
                if (housePayWayBean==null)
                {
                    dropDownMenu.setTabText("筛选",2);
                    //筛选回调
                    mPresenter.getHouseRentList(true, mPresenter.setParmat(houseRentListParmarBean,housePayWayBean));
                }else {
                    dropDownMenu.setTabText(housePayWayBean.getName(),2);
                    //筛选回调
                    mPresenter.getHouseRentList(true, mPresenter.setParmat(houseRentListParmarBean,housePayWayBean));
                }
            }
        });
        sxSelectHolderNew.initShow(this,housePayWayBeanType,housePayWayBeans);
    }

    /**
     * 筛选弹框初始化
     */
//    public void initSxpupowind() {
//        if (fytsSelectData == null || czyqAndfytsSelectData == null) {
//            return;
//        }
//        mhouseInfoTitleBeans = mPresenter.getData(czyqAndfytsSelectData, fytsSelectData);
//        popuwindowsSeelctSxRecyclerView.setLayoutManager(new LinearLayoutManager(HouseRentingShowDuanZuListActivity.this));
//        popuWindowSelectSxAdapter = new PopuWindowSelectSxAdapter(mhouseInfoTitleBeans);
//        popuwindowsSeelctSxRecyclerView.setAdapter(popuWindowSelectSxAdapter);
//        popuWindowSelectSxAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
//    }

    @Subscriber(mode = ThreadMode.MAIN)
    public void reciverLocation(HFLocationBean bdLocation) {
        showMessage("获取到定位信息了");
            houseRentingShowListAdapter.setMbdLocation(bdLocation);
        mbdLocation = bdLocation;
        houseRentListParmarBean.setLat(String.valueOf(bdLocation.getMlocation().getLatitude()));
        houseRentListParmarBean.setLgt(String.valueOf(bdLocation.getMlocation().getLongitude()));
    }

    /**
     * 开始定位
     * @return
     */
    public Location startLocation() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        // Criteria是一组筛选条件
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        //设置定位精准度
        criteria.setAltitudeRequired(false);////设置是否可以定位海拔,如果设置定位海拔，返回一定是gps
        //是否要求海拔
        criteria.setBearingRequired(true);
        //是否要求方向
        criteria.setCostAllowed(true);
        //是否要求收费
        criteria.setSpeedRequired(true);
        //是否要求速度
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        //设置电池耗电要求
        criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
        //设置方向精确度
        criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
        //设置速度精确度
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        //设置水平方向精确度
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
        //设置垂直方向精确度
        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
      return location;
    }
}
